﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Com.Seezt.Skins;
using Components.Enum;
using UI.Lib;
using Controls;
using Components;
using UI.Lib.Factory;

namespace UI.ShopManagement
{
    public partial class FrmBas1a22Edit : UI.ParentForm.BaseEdit
    {
        public FrmBas1a22Edit(string action, string text, string key)
            : base(action)
        {
            InitializeComponent();

            this.Text = text;
            this.key = key;
            //回写的方法
            this.SaveCallFuntion = "Bas1a22Manager$SaveData"; //保存的方法
            this.Size = base.MaxWindow;
            {
                LoadData();
            }

            this.tbSummary.SetValue("shopid", key);
        }

        //加载修改数据
        private void LoadData()
        {
            var where = string.Format(" a.shopid ='{0}' ", this.key);

            DataTable data = CommonHelper.Entity.LoadEditData(this.GetType().FullName, "E", "Summary", where);
            if (data == null || data.Rows.Count == 0)
            {
                //加载默认
                this.tbSummary.SetValue(ShopSetting.DirectStock, UI.Lib.Set.ShopSetting.DirectStock(""));
                this.tbSummary.SetValue(ShopSetting.NeedAudit, UI.Lib.Set.ShopSetting.NeedAudit(""));
                this.tbSummary.SetValue(ShopSetting.PrintPrice, UI.Lib.Set.ShopSetting.PrintPrice(""));
            }
            else
            {
                foreach (DataRow dr in data.Rows)
                {
                    var setName = dr["setname"].ToString();
                    var value = dr["value"].ToString();

                    this.tbSummary.SetValue(setName, value); //设置页面控件的值
                }
            }

            this.tbSummary.SaveBak();
        }

        protected override void AfterSaveData(bool isSuccess)
        {
            if (isSuccess)
            {
                UI.Lib.Set.ShopSetting.ClearAll();
            }
        }

        protected override void AfterSummaryCreateControl(string colName, Control c, Controls.Args.SummaryArgs e)
        {
            if ("needaudit" == colName)
            {
                var checkbox = c as CheckBox;
                if (checkbox == null) return;

                checkbox.CheckedChanged += checkbox_CheckedChanged;
            }
        }

        void checkbox_CheckedChanged(object sender, EventArgs e)
        {
            if (this.IsInitOk == false || this.Visible == false)
                return;

            var boxChecked = this.tbSummary.GetValue("needaudit");

            if ("Y" == boxChecked.ToString() & "Y" == this.tbSummary.GetValue("directstock").ToString())
            {
                MsgBox.Show("订单直接出库时，此选项会被忽略");
            }
        }
    }
}